<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/16
 * Time: 17:11
 */

namespace app\api\controller\v1\home;

use app\api\model\CartModel;
use app\api\model\GoodsModel;
use app\middleware\ApiAuth;
use think\Request;
use think\facade\Validate;
use think\response\Json;
use think\validate\ValidateRule;

/**
 * Class C端购物车控制器
 * @package app\controller
 */
class Cart
{
    protected $middleware = [ApiAuth::class];
    /**
     * @desc 购物车列表
     * @return Json
     */
    public function CartList(Request $request)
    {
        $input['uid'] = $request->param('uid');//用户ID
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return CartModel::CartList($input);
    }
    /**
     * @desc 购物车加减数量
     * @return Json
     */
    public function NumCart(Request $request)
    {

        $input['uid'] = $request->param('uid');//用户ID
        $input['goods_id'] = $request->param('goods_id');//商品ID
        $input['id'] = $request->param('id');//购物车ID
        $input['type'] = $request->param('type');//1:加 2：减
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(),
            'goods_id' => ValidateRule::isRequire(),
            'id' => ValidateRule::isRequire(),
            'type' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return CartModel::NumCart($input);
    }
    /**
     * @desc 添加购物车
     * @return Json
     */
    public function AddCart(Request $request)
    {
        $input['uid'] = $request->param('uid');//用户ID
        $input['cate_id'] = $request->param('cate_id');//分类ID
        $input['goods_id'] = $request->param('goods_id');//商品ID
        $input['goods_specific_id'] = $request->param('goods_specific_id');//规格ID
        $input['num'] = $request->param('num');//数量
        // 参数校验
        $validate = Validate::rule([
            'cate_id' => ValidateRule::isRequire(),
            'num' => ValidateRule::isRequire(),
            'goods_specific_id' => ValidateRule::isRequire(),
            'goods_id' => ValidateRule::isRequire(),
            'uid' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return CartModel::AddCart($input);
    }

    /**
     * @desc 删除购物车
     * @return Json
     */
    public function DelCart(Request $request)
    {
        $input['uid'] = $request->param('uid');//用户ID
        $input['id'] = $request->param('id');//购物车ID
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(),
            'uid' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return CartModel::DelCart($input);
    }
    /**
     * @desc 购物车数量
     * @return Json
     */
    public function CartNum(Request $request)
    {
        $input['uid'] = $request->param('uid');//用户ID
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return CartModel::CartNum($input);
    }
}